package com.xiao.dao;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiao.Utils.SqlSessionUtils;
import com.xiao.mapper.user2Mapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

public class reverseTest {
    @Test
    public void method(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
        user2 user21 = mapper.selectByPrimaryKey(1);
        System.out.println(user21);
    }
    @Test
    public void method1(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
        user2Example user2Example = new user2Example();
        /*调用createCriteria()来设置条件*/
        user2Example.createCriteria().andUsernameEqualTo("小黄").andPasswordEqualTo("12345");
        user2Example.or().andPasswordEqualTo("123");
        List<user2> select = mapper.selectByExample(user2Example);
        select.forEach(System.out::println);
        /*user2 user21 = mapper.selectByPrimaryKey(1);
        System.out.println(user21);*/
    }

    @Test
    public void updateByPrimaryKey(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
        user2 user2 = new user2(1,"小明","12345",2);
        mapper.updateByPrimaryKey(user2);
    }
    @Test
    public void updateByPrimaryKeySelective(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);

        user2 user2 = new user2(6,"小明","12345",null);
        /*updateByPrimaryKey:是按主键来更新数据,如果有一个属性值设置为null,那么数据显示的就是null*/
        mapper.updateByPrimaryKey(user2);

        user2 user3 = new user2(6,"小明","12345",1);
        /*updateByPrimaryKeySelective：按照主键来选择:你想更新哪个就更新哪个，哪个不想更新就设置为null*/
        mapper.updateByPrimaryKeySelective(user3);
    }
    @Test
    public void method3(){
        SqlSessionUtils sqlSessionUtils = new SqlSessionUtils();
        SqlSession sqlSession = sqlSessionUtils.SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
        /*user2Example user2Example = new user2Example();
        user2Example.createCriteria().andPasswordEqualTo("123");*/
        //查询开始前开启分页功能   /*pageNum；当前页码  pageSize:当前页数*/
        Page<Object> objects = PageHelper.startPage(2, 2);

        //显示objects的有关信息
        System.out.println(objects);

        List<user2> select = mapper.selectByExample(null);

        select.forEach(System.out::println);

/*
        //显示所有的页面中的信息
        PageInfo
        {pageNum=2, pageSize=2, size=2,
        startRow=3, endRow=4, total=5, pages=3,
        list=Page{count=true,
         pageNum=2, pageSize=2, startRow=2, endRow=4,
         total=5, pages=3, reasonable=false, pageSizeZero=false}
         [user2{id=3, username='小军', password='123', classId=1},
         user2{id=4, username='小黄', password='12345', classId=3}],
         prePage=1, nextPage=3, isFirstPage=false, isLastPage=false,
          hasPreviousPage=true, hasNextPage=true,
          navigatePages=3, navigateFirstPage=1, navigateLastPage=3, navigatepageNums=[1, 2, 3]}
*/
        PageInfo<Object> objectPageInfo = new PageInfo<>(select,3);

        System.out.println(objectPageInfo);
    }
    @Test
    public void insert(){
        SqlSession sqlSession = new SqlSessionUtils().SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
        user2 user21 = new user2(15, "xiaoming", "123", 3);
        mapper.insert(user21);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void DeleteMany(){
        SqlSession sqlSession = new SqlSessionUtils().SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
     //批量删除
        Integer[] i={11,12,13,15};
        mapper.Delete(i);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void InsertMany() {
        SqlSession sqlSession = new SqlSessionUtils().SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
        //批量添加
        user2 user21 = new user2(15, "xiaoming", "123", 3);
        user2 user22 = new user2(13, "xiaoming", "123", 3);
        user2 user23 = new user2(12, "xiaoming", "123", 3);
        user2 user24 = new user2(11, "xiaoming", "123", 3);
        List<user2> user2s = Arrays.asList(user21, user22, user23, user24);
       mapper.Insert(user2s);
       sqlSession.commit();
       sqlSession.close();
    }
    @Test
    public void SqlSession(){
        SqlSession sqlSession = new SqlSessionUtils().SqlSession();
        user2Mapper mapper = sqlSession.getMapper(user2Mapper.class);
        user2 user21 = mapper.selectByPrimaryKey(1);
        System.out.println(user21);
        sqlSession.clearCache();//手动清空SqlSession缓存
        user2 user22 = mapper.selectByPrimaryKey(1);
        System.out.println(user22);
    }
    @Test
    public void SqlSessionFactory1() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        user2Mapper mapper1 = sqlSession1.getMapper(user2Mapper.class);
         user2 user21 = mapper1.selectByPrimaryKey(1);
        System.out.println(user21);
        sqlSession1.close();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        user2Mapper mapper2 = sqlSession2.getMapper(user2Mapper.class);
        user2 user22 = mapper2.selectByPrimaryKey(1);
        System.out.println(user22);
    }
    @Test
    public void SqlSessionFactory(){
        SqlSession sqlSession1 = new SqlSessionUtils().SqlSession();
        user2Mapper mapper1 = sqlSession1.getMapper(user2Mapper.class);
        List<user2> user2s = mapper1.selectAll();
        sqlSession1.close();
        SqlSession sqlSession2 = new SqlSessionUtils().SqlSession();
        user2Mapper mapper2 = sqlSession2.getMapper(user2Mapper.class);
        List<user2> user2s1 = mapper2.selectAll();
        user2s1.forEach(System.out::println);
    }
}
